package com.hktf.moedokilock.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorSelectedListener;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.hktf.corelib.constant.KEYS;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.moedokilock.R;
import com.hktf.moedokilock.adapter.DataFormatItemAdapter;
import com.hktf.uilib.adapter.ColorItemAdapter;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.bean.ColorItem;
import com.hktf.uilib.bean.FontItem;
import com.hktf.uilib.bean.KeyValueItem;
import com.hktf.uilib.bean.lock.DateFormatItem;
import com.hktf.uilib.widget.PickColorPopWindow;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PickDateFormatPopWindow extends PopupWindow {
    private RecyclerView recyclerView;

    private DataFormatItemAdapter dataFormatItemAdapter;


    public PickDateFormatPopWindow(final BaseActivity context,MyListener listener) {
        this.myListener = listener;
        View rootView = View.inflate(context, R.layout.pick_date_format_pop, null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView = rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        rootView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        String[] formatterS = new String[]{
                "yyyy年MM月dd日,E"
                , "EEEE,M月d"
                , "yyyy/MM/dd"
                , "yyyy/MM/dd,E"
                , "EEEE,yyyy年MM月dd日"
                , "yyyy-MM-dd"
                , "yyyy-MM-dd,E"
                , "M月d,E"
        };
        List<DateFormatItem> fontItemList = Arrays.stream(formatterS)
                .map(i -> {
                    DateFormatItem fontItem = new DateFormatItem(false, i);
                    return fontItem;
                })
                .collect(Collectors.toList());

        dataFormatItemAdapter = new DataFormatItemAdapter(fontItemList, context);
        recyclerView.setAdapter(dataFormatItemAdapter);
        setContentView(rootView);
        setOutsideTouchable(true);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        dataFormatItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                DateFormatItem item = dataFormatItemAdapter.getItem(position);

                if (myListener != null) {
                    myListener.onPick(item.getFormatStr());
                }

            }
        });

    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (myListener != null) {
            myListener.onDismiss();
        }
    }


    private MyListener myListener;

    public MyListener getMyListener() {
        return myListener;
    }

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }

    public static interface MyListener {
        void onPick(String dateFormatStr);
        void onDismiss();

    }

    private OnCheckChangeListener onCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public static interface OnCheckChangeListener {
        void onCheckChange(KeyValueItem keyValueItem, int position);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }
}
